package com.salon.modules.sys.controller;

import com.salon.common.annotation.SysLog;
import com.salon.common.exception.CustomException;
import com.salon.common.http.response.ObjectResult;
import com.salon.common.http.response.ResultCode;
import com.salon.common.utils.Constant;
import com.salon.modules.sys.entity.SysMenuEntity;
import com.salon.modules.sys.service.ISysMenuService;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统菜单
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags="菜单管理")
public class SysMenuController extends BaseController {

    @Resource
    private ISysMenuService sysMenuService;

    @GetMapping("/list")
    @ApiOperation("分页")
    @RequiresPermissions("sys:menu:select")
    public ObjectResult<List<SysMenuEntity>> list() {
        List<SysMenuEntity> menuList = sysMenuService.list();
        HashMap<Long, SysMenuEntity> menuMap = new HashMap<>();
        for (SysMenuEntity s : menuList) {
            menuMap.put(s.getMenuId(), s);
        }
        for (SysMenuEntity s : menuList) {
            SysMenuEntity parent = menuMap.get(s.getParentId());
            if (Objects.nonNull(parent)) {
                s.setParentName(parent.getName());
            }
        }
        return ObjectResult.success(menuList);
    }

    @GetMapping("/select")
    @ApiOperation("全部查询")
    @RequiresPermissions("sys:menu:select")
    public ObjectResult<List<SysMenuEntity>> select() {
        // 查询列表数据
        List<SysMenuEntity> menuList = sysMenuService.listMenuNotButton();
        // 添加顶级菜单
        SysMenuEntity root = new SysMenuEntity();
        root.setMenuId(0L);
        root.setName("一级菜单");
        root.setParentId(-1L);
        root.setOpen(true);
        menuList.add(root);
        return ObjectResult.success(menuList);
    }

    @GetMapping("/info/{menuId}")
    @ApiOperation("详情")
    @RequiresPermissions("sys:menu:select")
    public ObjectResult<SysMenuEntity> info(@PathVariable("menuId") Long menuId) {
        SysMenuEntity menu = sysMenuService.getById(menuId);
        return ObjectResult.success(menu);
    }

    @SysLog("保存菜单")
    @PostMapping("/save")
    @ApiOperation("保存")
    @RequiresPermissions("sys:menu:save")
    public ObjectResult<String> save(@RequestBody SysMenuEntity menu) {
        //数据校验
        verifyForm(menu);
        sysMenuService.save(menu);
        return ObjectResult.success();
    }

    @SysLog("修改菜单")
    @PostMapping("/update")
    @ApiOperation("修改")
    @RequiresPermissions("sys:menu:update")
    public ObjectResult<String> update(@RequestBody SysMenuEntity menu) {
        //数据校验
        verifyForm(menu);
        sysMenuService.updateById(menu);
        return ObjectResult.success();
    }

    @SysLog("删除菜单")
    @PostMapping("/delete/{menuId}")
    @ApiOperation("删除")
    @RequiresPermissions("sys:menu:delete")
    public ObjectResult<String> delete(@PathVariable("menuId") long menuId) {
        // 设置系统固定菜单
        if (menuId <= 28) {
            return ObjectResult.failed(ResultCode.FAIL,"系统菜单，不能删除");
        }
        //判断是否有子菜单或按钮
        List<SysMenuEntity> menuList = sysMenuService.listMenuByParentId(menuId);
        if (menuList.size() > 0) {
            return ObjectResult.failed(ResultCode.FAIL,"请先删除子菜单或按钮");
        }
        sysMenuService.delete(menuId);
        return ObjectResult.success();
    }

    private void verifyForm(SysMenuEntity menu) {
        if (StringUtils.isBlank(menu.getName())) {
            throw new CustomException("菜单名称不能为空");
        }
        if (menu.getParentId() == null) {
            throw new CustomException("上级菜单不能为空");
        }
        // 菜单
        if (menu.getType() == Constant.MenuType.MENU.getValue() && StringUtils.isBlank(menu.getComponent())) {
            throw new CustomException("组件路径不能为空");
        }
        // 上级菜单类型
        int parentType = Constant.MenuType.CATALOG.getValue();
        if (menu.getParentId() != 0) {
            SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentId());
            parentType = parentMenu.getType();
        }
        // 目录、菜单
        if ((menu.getType() == Constant.MenuType.CATALOG.getValue() ||
                menu.getType() == Constant.MenuType.MENU.getValue()) && parentType != Constant.MenuType.CATALOG.getValue()) {
            throw new CustomException("上级菜单只能为目录类型");
        }
        // 按钮
        if (menu.getType() == Constant.MenuType.BUTTON.getValue() && parentType != Constant.MenuType.MENU.getValue()) {
            throw new CustomException("上级菜单只能为菜单类型");
        }
    }

}
